package com.itrip.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.itrip.config.AlipayConfig;
import com.itrip.dto.ResponseDTO;
import com.itrip.service.OrderService;
import com.itrip.vo.order.AlipayVO;
import com.itrip.vo.order.PayStatusVO;
import io.swagger.annotations.*;
import org.apache.solr.common.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "支付宝支付相关接口")
@ApiResponses({
        @ApiResponse(code = 200,message = "请求正常",response = ResponseDTO.class)
})
@RestController
public class AlipayController {

    @Resource
    private OrderService orderService;

    @ApiOperation("获取支付表单")
    @ApiImplicitParam(name = "token",value = "token令牌",dataType = "string",paramType = "header",required = true)
    @PostMapping("/view/getPayForm")
    public ResponseDTO getPayForm(AlipayVO alipayVO) throws AlipayApiException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);


        alipayRequest.setBizContent("{\"out_trade_no\":\""+ alipayVO.getOrderNo() +"\","
                + "\"total_amount\":\""+ alipayVO.getPayAmount() +"\","
                + "\"subject\":\""+ alipayVO.getOrderName() +"\","
                + "\"body\":\""+ alipayVO.getOrderDesc() +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");


        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        //输出
        return new ResponseDTO(100,"获取支付订单成功",result);
    }

    @ApiOperation("获取订单支付状态")
    @ApiImplicitParam(name = "token",value = "token令牌",dataType = "string",paramType = "header",required = true)
    @PostMapping("/view/getPayStatus")
    public ResponseDTO getPayStatus(PayStatusVO payStatusVO) throws AlipayApiException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();

        //请二选一设置

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ payStatusVO.getOrderNo() +"\","+"\"trade_no\":\""+ payStatusVO.getTradeNo() +"\"}");

        //请求
        String result = alipayClient.execute(alipayRequest).getBody();
        JSONObject response = JSONObject.parseObject(result).getJSONObject("alipay_trade_query_response");
        String code = response.getString("code");
        if (!StringUtils.isEmpty(code) && code.equals("10000")){

            int count = orderService.updateOrderStatus(payStatusVO);
            return new ResponseDTO(100,"订单已支付成功",null);
        }

        return new ResponseDTO(101,response.getString("sub_msg"),null);
    }
}
